package org.example.controller;

import org.example.domain.dto.ShoppingCartInfo;
import org.example.domain.dto.ShoppingCartNumInfo;
import org.example.domain.model.Result;
import org.example.service.impl.ShoppingCartAllInfoServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartAllInfoController {

    @Resource
    private ShoppingCartAllInfoServiceImpl shoppingCartAllInfoService;

//    public ShoppingCartAllInfoController(ShoppingCartAllInfoServiceImpl shoppingCartAllInfoService) {
//        this.shoppingCartAllInfoService = shoppingCartAllInfoService;
//    }

    @GetMapping("/findAll")
    public Result findAll( String userName){
        return Result.buildSuccess(shoppingCartAllInfoService.selectAllShoppingCart(userName));
    }

    @PostMapping("/add")
    public Result addShoppingCart(ShoppingCartInfo shoppingCartInfo){
//        System.out.println(shoppingCartInfo);
        int id = shoppingCartAllInfoService.insertShoppingCart(shoppingCartInfo);
        if(id == 0){
            return Result.buildFail("fail");
        }
        return Result.buildSuccess(id,"success");
    }

    @GetMapping("/addNum")
    public Result addShoppingCartNum(Integer shoppingCartId){
        int i = shoppingCartAllInfoService.addShoppingCartNum(shoppingCartId);
        if(i == 1){
            return Result.buildSuccess(i,"success");
        }else {
            return Result.buildFail("fail");
        }
    }
    @GetMapping("/minusNum")
    public Result minusShoppingCartNum(Integer shoppingCartId){
        int i = shoppingCartAllInfoService.minusShoppingCartNum(shoppingCartId);
        if(i == 1){
            return Result.buildSuccess(i,"success");
        }else {
            return Result.buildFail("fail");
        }
    }

    @GetMapping("/assignNum")
    public Result assignNumShoppingCartNum(ShoppingCartNumInfo shoppingCartNumInfo){
        System.out.println(shoppingCartNumInfo.getShoppingCartId());
        System.out.println(shoppingCartNumInfo.getChangeNum());
        int i = shoppingCartAllInfoService.assignShoppingCartNum(shoppingCartNumInfo);
        if(i == 1){
            return Result.buildSuccess(i,"success");
        }else {
            return Result.buildFail("fail");
        }
    }

    @GetMapping("/deleteCart")
    public Result deleteShoppingCart(Integer shoppingCartId){
        int i = shoppingCartAllInfoService.deleteShoppingCart(shoppingCartId);
        if(i == 1){
            return Result.buildSuccess(i,"success");
        } else {
            return Result.buildFail("fail");
        }
    }

    @PostMapping("/deleteCheckedCart")
    public Result deleteCheckedShoppingCart(String checkedShoppingCartId){
//        System.out.println("asdsfhjaglhjeudskjvifdskjv"+checkedShoppingCartId);
//        System.out.println("@#$@#$@#$@$#%$@#$@#======" + checkedShoppingCartId);
        int i = shoppingCartAllInfoService.deleteCheckedShoppingCart(checkedShoppingCartId);
        if(i > 0){
            return Result.buildSuccess(i,"success");
        }
        return Result.buildFail("fail");
    }

    @GetMapping("/count")
    public Result countShoppingCart(String userName){
        if(userName != null){
            return Result.buildSuccess(shoppingCartAllInfoService.countShoppingCart(userName),"success");

        }else {
            return Result.buildFail("fail");
        }
//        System.out.println("234sdfw32rfwefr2edf234r======================="+userName);
    }

}
